<?php

/**
 * 每天定时更新盲盒奖品的物流信息
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brandActivity\BlindBoxLotteryConsignee;
use Biz\model\trade\ShopCourier as mShopCourier;
use Biz\module\Packages\Logistics\TrackingMore;
use Biz\service\brand\BlindBoxActivity;

$list = BlindBoxLotteryConsignee::selectByWhere(['consignee_state[!]' => BlindBoxLotteryConsignee::CONSIGNEE_SUCCESS]);
$courseIds = array_column($list, 'courier_id');
$courierList = mShopCourier::getCourierList(['courier_id', 'trackingmore_code'], ['courier_id' => $courseIds]);
$courierList = array_column($courierList, null, 'courier_id');
foreach ($list as $row) {
    if (isset($courierList[$row['courier_id']])) {
        $carrierCode = $courierList[$row['courier_id']]['trackingmore_code'];
        $trackingNumber = $row['express_no'];
        $res = TrackingMore::getTrackingResult($carrierCode, $trackingNumber);
        if ($res && $res['meta']['code'] == 200 && $res['data']) {
            $status = 0;
            switch ($res['data']['status']) {
                case 'transit':
                    $status = BlindBoxLotteryConsignee::CONSIGNEE_TRANSITING;
                    break;
                case 'delivered':
                    $status = BlindBoxLotteryConsignee::CONSIGNEE_SUCCESS;
                    break;
                default:
                    $status = BlindBoxLotteryConsignee::CONSIGNEE_NORMAL;
                    break;
            }
            BlindBoxLotteryConsignee::updateData(['consignee_state' => $status], ['id' => $row['id']]);
        }
    }
}
//发货提醒
BlindBoxActivity::shippingRemind();